package edu.scau.divineplace.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import edu.scau.divineplace.entity.dos.*;
import edu.scau.divineplace.entity.enums.BaseEnum;
import edu.scau.divineplace.entity.enums.IsReadEnum;
import edu.scau.divineplace.entity.enums.LikeTargetTypeEnum;
import edu.scau.divineplace.entity.enums.RoleTypeEnum;
import edu.scau.divineplace.entity.vo.SearchResultVO;
import edu.scau.divineplace.entity.vo.content.ContentInfomationVO;
import edu.scau.divineplace.entity.vo.content.RecentWorkVO;
import edu.scau.divineplace.util.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * @Author: x1aolone
 * @Date: 2020/12/9 13:33
 */
@Service
public class LikeService {

    @Autowired
    private ImportMapperService importMapperService;

    /**
     * 通过用户id、点赞内容id、点赞内容类型, 判断用户是否点赞内容
     * @param userId
     * @param likeTargetId
     * @param likeTargetType
     * @return
     */
    public Boolean judge(Long userId, Long likeTargetId, Integer likeTargetType){
        LikeTargetTypeEnum likeTargetTypeEnum = BaseEnum.typeOf(LikeTargetTypeEnum.class, likeTargetType);
        Preconditions.checkNotNull(likeTargetTypeEnum);
        LambdaQueryWrapper<LikeDO> query = Wrappers.<LikeDO>lambdaQuery()
                .eq(LikeDO::getUserId, userId)
                .eq(LikeDO::getLikeTargetId, likeTargetId)
                .eq(LikeDO::getLikeTargetType, likeTargetTypeEnum);
        Integer count = importMapperService.likeMapper.selectCount(query);
        Boolean isExist = count > 0;
        return isExist;
    }

    /**
     * 用户点赞内容
     * @param userId
     * @param likeTargetUserId
     * @param likeTargetType
     * @param likeTargetId
     */
    public void like (Long userId, Long likeTargetUserId, Integer likeTargetType, Long likeTargetId) {
        LikeTargetTypeEnum likeTargetTypeEnum = BaseEnum.typeOf(LikeTargetTypeEnum.class, likeTargetType);
        Preconditions.checkNotNull(likeTargetTypeEnum);
        LikeDO DO = new LikeDO();
        DO.setUserId(userId);
        DO.setLikeTargetUserId(likeTargetUserId);
        DO.setLikeTargetType(likeTargetTypeEnum);
        DO.setLikeTargetId(likeTargetId);
        importMapperService.likeMapper.insert(DO);
    }


    /**
     * 用户取消点赞内容
     * @param userId
     * @param likeTargetId
     * @param likeTargetType
     */
    public void unlike (Long userId, Long likeTargetId, Integer likeTargetType) {
        LikeTargetTypeEnum likeTargetTypeEnum = BaseEnum.typeOf(LikeTargetTypeEnum.class, likeTargetType);
        Preconditions.checkNotNull(likeTargetTypeEnum);
        LambdaQueryWrapper<LikeDO> query = Wrappers.<LikeDO>lambdaQuery()
                .eq(LikeDO::getUserId, userId)
                .eq(LikeDO::getLikeTargetId, likeTargetId)
                .eq(LikeDO::getLikeTargetType, likeTargetTypeEnum);
        importMapperService.likeMapper.delete(query);
    }

    /**
     * 查询该内容的点赞数
     * @param likeTargetType
     * @param likeTargetId
     * @return
     */
    public Integer count(LikeTargetTypeEnum likeTargetType, Long likeTargetId) {
        LambdaQueryWrapper<LikeDO> likeQuery = Wrappers.<LikeDO>lambdaQuery()
                .eq(LikeDO::getLikeTargetType, likeTargetType)
                .eq(LikeDO::getLikeTargetId, likeTargetId);
        Integer count = importMapperService.likeMapper.selectCount(likeQuery);
        return count;
    }

}
